package com.baidu.lzk.dinnerchooser;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class BlackListActivity extends Activity implements OnItemClickListener
{
	private RestaurantDatabaseHelper helper;
	//private Cursor cursor;
	private ListView list;
	private Cursor cursor;

	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blacklist);

		helper = RestaurantDatabaseHelper.getInstance(this);

		cursor = helper.selectAll();

 		list = (ListView) findViewById(R.id.blacklist_view);
		list.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cursor,
				new String[] { Restaurant.COLUMNS_NAME }, new int[] { android.R.id.text1 }));
		list.setOnItemClickListener(this);

		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		cursor.moveToFirst();
		while (!cursor.isLast())
		{
			long blacklist = cursor.getLong(cursor.getColumnIndex(Restaurant.COLUMNS_BLACKLIST));
			if (blacklist > 0)
			{
				list.setItemChecked(cursor.getPosition(), true);
			}
			cursor.moveToNext();
		}
		//cursor.close();
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();

		SparseBooleanArray checkedArray = list.getCheckedItemPositions();
		
		for(int i = 0; i < list.getCount(); i++)
		{
			boolean checked = checkedArray.get(i);
			//Log.d("lzk", "checked = " + i + " # " + checked);
			if(checked)
			{
				helper.updateBlackList(i, System.currentTimeMillis());
			}
			else
			{
				helper.updateBlackList(i, 0);
			}
		}
		
//		cursor.close();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	{
		//((CheckedTextView) arg1).toggle();
//		boolean isChecked = ((CheckedTextView) arg1).isChecked();
//		 Log.d("lzk", "count = " + arg2 + "||" + isChecked);
//		 list.get
	}
}
